{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/WASasquatch/was-node-suite-comfyui/blob/main/ComfyUI_%2B_WAS_Node_Suite_and_ComfyUI_Manager.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "aaaaaaaaaa"
      },
      "source": [
        "# <font color=\"#0497e0\">**Comfy**</font>UI + <font color=\"#4684a3\">**WAS** Node Suite</font> &nbsp; [![Hits](https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fcolab.research.google.com%2Fgithub%2FWASasquatch%2Fcomfyui-colab-was-node-suite%2Fblob%2Fmain%2FComfyUI_%252B_WAS_Node_Suite.ipynb&count_bg=%23EAAC00&title_bg=%233092C6&icon=&icon_color=%23E7E7E7&title=hits&edge_flat=false)](https://hits.seeyoufarm.com)\n",
        "A version of ComfyUI Colab with WAS Node Suite installatoin."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "bbbbbbbbbb",
        "cellView": "form"
      },
      "outputs": [],
      "source": [
        "#@title <font color=\"orange\" size=\"5\">Environment Setup</font>\n",
        "#@markdown <font color=\"#0497e0\">Download and install ComfyUI + WAS Node Suite. You can run this cell again with the `UPDATE_COMFY_UI` or `UPDATE_WAS_NS` options selected to update.\n",
        "\n",
        "from pathlib import Path\n",
        "import time\n",
        "\n",
        "OPTIONS = {}\n",
        "\n",
        "#@markdown <font color=\"#48768c\">Store ComfyUI on Google Drive instead of Colab</font>\n",
        "USE_GOOGLE_DRIVE = True  #@param {type:\"boolean\"}\n",
        "#markdown <font color=\"#48768c\">Update ComfyUI</font>\n",
        "UPDATE_COMFY_UI = True  #@param {type:\"boolean\"}\n",
        "#@markdown <font color=\"#48768c\">Update WAS Node Suite</font>\n",
        "UPDATE_WAS_NS = True #@param {type:\"boolean\"}\n",
        "#@markdown <font color=\"#48768c\">Update Pillow for WAS NS:</font>\n",
        "UPDATE_PILLOW = False #@param {type:\"boolean\"}\n",
        "#@markdown <font color=\"#48768c\">ComfyUI Manager:</font>\n",
        "USE_COMFYUI_MANAGER = True #@param {type:\"boolean\"}\n",
        "UPDATE_COMFYUI_MANAGER = True #@param {type:\"boolean\"}\n",
        "\n",
        "WORKSPACE = '/content/ComfyUI'\n",
        "OPTIONS['USE_GOOGLE_DRIVE'] = USE_GOOGLE_DRIVE\n",
        "OPTIONS['UPDATE_COMFY_UI'] = UPDATE_COMFY_UI\n",
        "\n",
        "if USE_GOOGLE_DRIVE:\n",
        "    !echo \"Mounting Google Drive...\"\n",
        "    %cd /\n",
        "    from google.colab import drive\n",
        "    drive.mount('/content/drive')\n",
        "    WORKSPACE = \"/content/drive/MyDrive/ComfyUI\"\n",
        "    %cd /content/drive/MyDrive\n",
        "\n",
        "![ ! -d $WORKSPACE ] && echo -= Initial setup ComfyUI =- && git clone https://github.com/comfyanonymous/ComfyUI $WORKSPACE\n",
        "%cd $WORKSPACE\n",
        "\n",
        "if UPDATE_COMFY_UI:\n",
        "  !echo -= Updating ComfyUI =-\n",
        "  !git pull\n",
        "\n",
        "!echo -= Install dependencies =-\n",
        "!pip install xformers -r requirements.txt --extra-index-url https://download.pytorch.org/whl/cu118\n",
        "!git clone https://github.com/WASasquatch/was-node-suite-comfyui $WORKSPACE/custom_nodes/was-node-suite-comfyui\n",
        "\n",
        "if USE_COMFYUI_MANAGER:\n",
        "    !git clone https://github.com/ltdrdata/ComfyUI-Manager.git $WORKSPACE/custom_nodes/ComfyUI-Manager\n",
        "\n",
        "if UPDATE_WAS_NS:\n",
        "    %cd $WORKSPACE/custom_nodes/was-node-suite-comfyui\n",
        "    !git pull\n",
        "    %cd $WORKSPACE\n",
        "\n",
        "if UPDATE_COMFYUI_MANAGER:\n",
        "    %cd $WORKSPACE/custom_nodes/ComfyUI-Manager\n",
        "    !git pull\n",
        "    %cd $WORKSPACE\n",
        "\n",
        "if UPDATE_PILLOW:\n",
        "    !pip install --upgrade --force-reinstall pillow\n",
        "    print('\\n\\033[91m\\033[1mRestarting runtime for Pillow Update. Run this cell again without `UPDATE_PILLOW` selected!\\033[0m')\n",
        "    time.sleep(5)\n",
        "\n",
        "    import os\n",
        "    os.kill(os.getpid(), 9)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cccccccccc"
      },
      "source": [
        "Download some models/checkpoints/vae or custom comfyui nodes (uncomment the commands for the ones you want)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "dddddddddd",
        "cellView": "form"
      },
      "outputs": [],
      "source": [
        "#@title <font color=\"orange\" size=\"5\">Download Models</font>\n",
        "#@markdown <font color=\"#0497e0\">Download models and other resources to use in ComfyUI. Select your options and run the this cell. Can run multiple times with different options.</font>\n",
        "\n",
        "#@markdown ---\n",
        "\n",
        "# Checkpoints\n",
        "\n",
        "MODEL_OPTION = 'stable-diffusion-xl-base-1.0.safetensors' #@param['None', 'stable-diffusion-xl-base-1.0.safetensors', 'sd_xl_base_1.0_0.9vae.safetensors', 'sd_xl_base_0.9.safetensors', 'v1-5-pruned-emaonly.ckpt', 'v2-1_512-ema-pruned.safetensors', 'v2-1_768-ema-pruned.safetensors', 'AbyssOrangeMix2_hard.safetensors', 'AOM3A1_orangemixs.safetensors', 'AOM3A3_orangemixs.safetensors', 'wd-1-5-beta2-fp16.safetensors']\n",
        "VAE_OPTION = 'vae-ft-mse-840000-ema-pruned.safetensors' #@param['None', 'vae-ft-mse-840000-ema-pruned.safetensors', 'orangemix.vae.pt', 'kl-f8-anime2.ckpt']\n",
        "UPSCALE_MODEL_OPTION = 'None' #@param['None', 'RealESRGAN_x2.pth', 'RealESRGAN_x4.pth', '4x-UltraSharp', '4x_RealisticRescaler_100000_G.pth', 'BSRGAN.pth', 'BSRGANx2.pth']\n",
        "LORA_OPTION = 'None' #@param['None', 'theovercomer8sContrastFix_sd21768.safetensors', 'theovercomer8sContrastFix_sd15.safetensors']\n",
        "T2I_OPTION = 'None' #@param['None', 't2iadapter_depth_sd14v1.pth', 't2iadapter_seg_sd14v1.pth', 't2iadapter_sketch_sd14v1.pth', 't2iadapter_keypose_sd14v1.pth', 't2iadapter_openpose_sd14v1.pth', 't2iadapter_color_sd14v1.pth', 't2iadapter_canny_sd14v1.pth', '/t2iadapter_style_sd14v1.pth']\n",
        "CONTROLNET_OPTION = 'None' #@param['None', 'control_depth-fp16.safetensors', 'control_scribble-fp16.safetensors', 'control_openpose-fp16.safetensors']\n",
        "\n",
        "#@markdown ---\n",
        "\n",
        "#@markdown <font color=\"#0497e0\">**Download and instlal CLIPVision**:</font>\n",
        "DOWNLOAD_CLIPVISION = False #@param {type:\"boolean\"}\n",
        "#@markdown <font color=\"#0497e0\">**ControlNet Preprocessor Nodes** by Fannovel16:</font>\n",
        "INSTALL_CONTROLNET_NODES = False #@param {type:\"boolean\"}\n",
        "\n",
        "# SDXL\n",
        "if MODEL_OPTION == 'stable-diffusion-xl-base-1.0.safetensors':\n",
        "    !wget -c https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0.safetensors -P $WORKSPACE/models/checkpoints/\n",
        "if MODEL_OPTION == 'sd_xl_base_1.0_0.9vae.safetensors':\n",
        "    !wget -c https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0_0.9vae.safetensors -P $WORKSPACE/models/checkpoints/\n",
        "if MODEL_OPTION == 'sd_xl_base_0.9.safetensors':\n",
        "    !wget -c https://huggingface.co/stabilityai/stable-diffusion-xl-base-0.9/resolve/main/sd_xl_base_0.9.safetensors -P $WORKSPACE/models/checkpoints/\n",
        "\n",
        "\n",
        "# SD1.5\n",
        "if MODEL_OPTION == 'v1-5-pruned-emaonly.ckpt':\n",
        "    !wget -c https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.ckpt -P $WORKSPACE/models/checkpoints/\n",
        "\n",
        "# SD2\n",
        "if MODEL_OPTION == 'v2-1_512-ema-pruned.safetensors':\n",
        "    !wget -c https://huggingface.co/stabilityai/stable-diffusion-2-1-base/resolve/main/v2-1_512-ema-pruned.safetensors -P $WORKSPACE/models/checkpoints/\n",
        "if MODEL_OPTION == 'v2-1_768-ema-pruned.safetensors':\n",
        "    !wget -c https://huggingface.co/stabilityai/stable-diffusion-2-1/resolve/main/v2-1_768-ema-pruned.safetensors -P $WORKSPACE/models/checkpoints/\n",
        "\n",
        "# Some SD1.5 anime style\n",
        "if MODEL_OPTION == 'AbyssOrangeMix2_hard.safetensors':\n",
        "    !wget -c https://huggingface.co/WarriorMama777/OrangeMixs/resolve/main/Models/AbyssOrangeMix2/AbyssOrangeMix2_hard.safetensors -P $WORKSPACE/models/checkpoints/\n",
        "if MODEL_OPTION == 'AOM3A1_orangemixs.safetensors':\n",
        "    !wget -c https://huggingface.co/WarriorMama777/OrangeMixs/resolve/main/Models/AbyssOrangeMix3/AOM3A1_orangemixs.safetensors -P $WORKSPACE/models/checkpoints/\n",
        "if MODEL_OPTION == 'AOM3A3_orangemixs.safetensors':\n",
        "    !wget -c https://huggingface.co/WarriorMama777/OrangeMixs/resolve/main/Models/AbyssOrangeMix3/AOM3A3_orangemixs.safetensors -P $WORKSPACE/models/checkpoints/\n",
        "if MODEL_OPTION == 'anything-v3-fp16-pruned.safetensors':\n",
        "    !wget -c https://huggingface.co/Linaqruf/anything-v3.0/resolve/main/anything-v3-fp16-pruned.safetensors -P $WORKSPACE/models/checkpoints/\n",
        "\n",
        "# Waifu Diffusion 1.5 (anime style SD2.x 768-v)\n",
        "if MODEL_OPTION == 'wd-1-5-beta2-fp16.safetensors':\n",
        "    !wget -c https://huggingface.co/waifu-diffusion/wd-1-5-beta2/resolve/main/checkpoints/wd-1-5-beta2-fp16.safetensors -P $WORKSPACE/models/checkpoints/\n",
        "\n",
        "\n",
        "# VAE\n",
        "if VAE_OPTION == 'vae-ft-mse-840000-ema-pruned.safetensors':\n",
        "    !wget -c https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors -P $WORKSPACE/models/vae/\n",
        "if VAE_OPTION == 'orangemix.vae.pt':\n",
        "    !wget -c https://huggingface.co/WarriorMama777/OrangeMixs/resolve/main/VAEs/orangemix.vae.pt -P $WORKSPACE/models/vae/'\n",
        "if VAE_OPTION == 'kl-f8-anime2.ckpt':\n",
        "    !wget -c https://huggingface.co/hakurei/waifu-diffusion-v1-4/resolve/main/vae/kl-f8-anime2.ckpt -P $WORKSPACE/models/vae/\n",
        "\n",
        "\n",
        "# Loras\n",
        "if LORA_OPTION == 'theovercomer8sContrastFix_sd21768.safetensors':\n",
        "    !wget -c https://civitai.com/api/download/models/10350 -O $WORKSPACE/models/loras/theovercomer8sContrastFix_sd21768.safetensors #theovercomer8sContrastFix SD2.x 768-v\n",
        "if LORA_OPTION == 'theovercomer8sContrastFix_sd15.safetensors':\n",
        "    !wget -c https://civitai.com/api/download/models/10638 -O $WORKSPACE/models/loras/theovercomer8sContrastFix_sd15.safetensors #theovercomer8sContrastFix SD1.x\n",
        "\n",
        "\n",
        "# T2I-Adapter\n",
        "if T2I_OPTION == 't2iadapter_depth_sd14v1.pth':\n",
        "    !wget -c https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/t2iadapter_depth_sd14v1.pth -P $WORKSPACE/models/controlnet/\n",
        "if T2I_OPTION == 't2iadapter_seg_sd14v1.pth':\n",
        "    !wget -c https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/t2iadapter_seg_sd14v1.pth -P $WORKSPACE/models/controlnet/\n",
        "if T2I_OPTION == 't2iadapter_sketch_sd14v1.pth':\n",
        "    !wget -c https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/t2iadapter_sketch_sd14v1.pth -P $WORKSPACE/models/controlnet/\n",
        "if T2I_OPTION == 't2iadapter_keypose_sd14v1.pth':\n",
        "    !wget -c https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/t2iadapter_keypose_sd14v1.pth -P $WORKSPACE/models/controlnet/\n",
        "if T2I_OPTION == 't2iadapter_openpose_sd14v1.pth':\n",
        "    !wget -c https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/t2iadapter_openpose_sd14v1.pth -P $WORKSPACE/models/controlnet/\n",
        "if T2I_OPTION == 't2iadapter_color_sd14v1.pth':\n",
        "    !wget -c https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/t2iadapter_color_sd14v1.pth -P $WORKSPACE/models/controlnet/\n",
        "if T2I_OPTION == 't2iadapter_canny_sd14v1.pth':\n",
        "    !wget -c https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/t2iadapter_canny_sd14v1.pth -P $WORKSPACE/models/controlnet/\n",
        "\n",
        "# T2I Styles Model\n",
        "if T2I_OPTION == '/t2iadapter_style_sd14v1.pth':\n",
        "    !wget -c https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/t2iadapter_style_sd14v1.pth -P $WORKSPACE/models/style_models/\n",
        "\n",
        "# CLIPVision model (needed for styles model)\n",
        "if DOWNLOAD_CLIPVISION:\n",
        "    !wget -c https://huggingface.co/openai/clip-vit-large-patch14/resolve/main/pytorch_model.bin -O $WORKSPACE/models/clip_vision/clip_vit14.bin\n",
        "\n",
        "\n",
        "# ControlNet\n",
        "if CONTROLNET_OPTION == 'control_depth-fp16.safetensors':\n",
        "    !wget -c https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_depth-fp16.safetensors -P $WORKSPACE/models/controlnet/\n",
        "if CONTROLNET_OPTION == 'control_scribble-fp16.safetensors':\n",
        "    !wget -c https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_scribble-fp16.safetensors -P $WORKSPACE/models/controlnet/\n",
        "if CONTROLNET_OPTION == 'control_openpose-fp16.safetensors':\n",
        "    !wget -c https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_openpose-fp16.safetensors -P $WORKSPACE/models/controlnet/\n",
        "\n",
        "\n",
        "# Controlnet Preprocessor nodes by Fannovel16\n",
        "if INSTALL_CONTROLNET_NODES:\n",
        "    !cd custom_nodes && git clone https://github.com/Fannovel16/comfy_controlnet_preprocessors; cd comfy_controlnet_preprocessors && python install.py\n",
        "\n",
        "# ESRGAN upscale model\n",
        "if UPSCALE_MODEL_OPTION == 'RealESRGAN_x2.pth':\n",
        "    !wget -c https://huggingface.co/sberbank-ai/Real-ESRGAN/resolve/main/RealESRGAN_x2.pth -P $WORKSPACE/models/upscale_models/\n",
        "if UPSCALE_MODEL_OPTION == 'RealESRGAN_x4.pth':\n",
        "    !wget -c https://huggingface.co/sberbank-ai/Real-ESRGAN/resolve/main/RealESRGAN_x4.pth -P $WORKSPACE/models/upscale_models/\n",
        "if UPSCALE_MODEL_OPTION == '4x-UltraSharp':\n",
        "    !wget -c https://huggingface.co/uwg/upscaler/resolve/main/ESRGAN/4x-UltraSharp.pth -P $WORKSPACE/models/upscale_models/\n",
        "if UPSCALE_MODEL_OPTION == '4x_RealisticRescaler_100000_G.pth':\n",
        "    !wget -c https://huggingface.co/uwg/upscaler/resolve/main/ESRGAN/4x_RealisticRescaler_100000_G.pth -P $WORKSPACE/models/upscale_models/\n",
        "if UPSCALE_MODEL_OPTION == 'BSRGAN.pth':\n",
        "    !wget -c https://huggingface.co/uwg/upscaler/resolve/main/ESRGAN/BSRGAN.pth -P $WORKSPACE/models/upscale_models/\n",
        "if UPSCALE_MODEL_OPTION == 'BSRGANx2.pth':\n",
        "    !wget -c https://huggingface.co/uwg/upscaler/resolve/main/ESRGAN/BSRGANx2.pth -P $WORKSPACE/models/upscale_models/\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#@title <font size=\"5\" color=\"orange\">Direct Download Models</font>\n",
        "\n",
        "import os\n",
        "\n",
        "types = {\n",
        "    'CHECKPOINTS': os.path.join(WORKSPACE, 'models/checkpoints'),\n",
        "    'CLIP': os.path.join(WORKSPACE, 'models/clip'),\n",
        "    'CLIP_VISION': os.path.join(WORKSPACE, 'models/clip_vision'),\n",
        "    'CONFIGS': os.path.join(WORKSPACE, 'models/configs'),\n",
        "    'CONTROLNET': os.path.join(WORKSPACE, 'models/controlnet'),\n",
        "    'DIFFUSERS': os.path.join(WORKSPACE, 'models/diffusers'),\n",
        "    'EMBEDDINGS': os.path.join(WORKSPACE, 'models/embeddings'),\n",
        "    'GLIGEN': os.path.join(WORKSPACE, 'models/gligen'),\n",
        "    'HYPERNETWORKS': os.path.join(WORKSPACE, 'models/hypernetworks'),\n",
        "    'LORAS': os.path.join(WORKSPACE, 'models/loras'),\n",
        "    'STYLE_MODEL': os.path.join(WORKSPACE, 'models/style_models'),\n",
        "    'UNET': os.path.join(WORKSPACE, 'models/unet'),\n",
        "    'UPSCALE_MODELS': os.path.join(WORKSPACE, 'models/upscale_models'),\n",
        "    'VAE': os.path.join(WORKSPACE, 'models/vae'),\n",
        "    'VAE_APPROX': os.path.join(WORKSPACE, 'models/vae_approx')\n",
        "}\n",
        "\n",
        "TYPE = 'CHECKPOINTS' #@param ['CHECKPOINTS', 'CLIP', 'CLIP_VISION', 'CONFIGS', 'CONTROLNET', 'DIFFUSERS', 'EMBEDDINGS', 'GLIGEN', 'HYPERNETWORKS', 'LORAS', 'STYLE_MODEL', 'UNET', 'UPSCALE_MODELS', 'VAE', 'VAE_APPROX']\n",
        "DIRECT_URL = 'https://civitai.com/api/download/models/141627' #@param {type:\"string\"}\n",
        "SAVE_AS = 'Differentia_V1.safetensors' #@param {type: 'string'}\n",
        "#@markdown <font color=\"0497e0\">Direct link to the download. The example URL is Differentia (https://civitai.com/models/129232)</font>\n",
        "\n",
        "if TYPE and DIRECT_URL:\n",
        "        target = os.path.join(types[TYPE], SAVE_AS)\n",
        "        !wget -c $DIRECT_URL -O $target"
      ],
      "metadata": {
        "cellView": "form",
        "id": "Bo0pf4So3tCK"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#@title <font size=\"5\" color=\"orange\">ComfyUI Cloudfare (Recommended)</font>\n",
        "#@markdown <font color=\"0497e0\">Running ComfyUI with Cloudfare is now the recommended method.</font>\n",
        "!wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb\n",
        "!dpkg -i cloudflared-linux-amd64.deb\n",
        "\n",
        "import subprocess\n",
        "import threading\n",
        "import time\n",
        "import socket\n",
        "import urllib.request\n",
        "\n",
        "def iframe_thread(port):\n",
        "  while True:\n",
        "      time.sleep(0.5)\n",
        "      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
        "      result = sock.connect_ex(('127.0.0.1', port))\n",
        "      if result == 0:\n",
        "        break\n",
        "      sock.close()\n",
        "  print(\"\\nComfyUI finished loading, trying to launch cloudflared (if it gets stuck here cloudflared is having issues)\\n\")\n",
        "\n",
        "  p = subprocess.Popen([\"cloudflared\", \"tunnel\", \"--url\", \"http://127.0.0.1:{}\".format(port)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n",
        "  for line in p.stderr:\n",
        "    l = line.decode()\n",
        "    if \"trycloudflare.com \" in l:\n",
        "      print(\"This is the URL to access ComfyUI:\", l[l.find(\"http\"):], end='')\n",
        "    #print(l, end='')\n",
        "\n",
        "threading.Thread(target=iframe_thread, daemon=True, args=(8188,)).start()\n",
        "\n",
        "!python main.py --dont-print-server"
      ],
      "metadata": {
        "cellView": "form",
        "id": "StSynv5tp2nL"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "jjjjjjjjjjjjj",
        "cellView": "form"
      },
      "outputs": [],
      "source": [
        "#@title <font size=\"5\" color=\"orange\">ComfyUI Localtunnel</font>\n",
        "#@markdown <font color=\"0497e0\">Run this cell to start ComfyUI. You'll see a link similar to `your url is: https://slow-yaks-jog-34-72-173-3.loca.lt` (example)</font>\n",
        "#@markdown <br>*If you have trouble with the red screen of death \"reminder\" not letting you generate, use the iFrame version below.*\n",
        "!npm install -g localtunnel\n",
        "\n",
        "import subprocess\n",
        "import threading\n",
        "import time\n",
        "import socket\n",
        "import urllib.request\n",
        "\n",
        "def iframe_thread(port):\n",
        "  while True:\n",
        "      time.sleep(0.5)\n",
        "      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
        "      result = sock.connect_ex(('127.0.0.1', port))\n",
        "      if result == 0:\n",
        "        break\n",
        "      sock.close()\n",
        "  print(\"\\nComfyUI finished loading, trying to launch localtunnel (if it gets stuck here localtunnel is having issues)\\n\")\n",
        "\n",
        "  print(\"The password/enpoint ip for localtunnel is:\", urllib.request.urlopen('https://ipv4.icanhazip.com').read().decode('utf8').strip(\"\\n\"))\n",
        "  p = subprocess.Popen([\"lt\", \"--port\", \"{}\".format(port)], stdout=subprocess.PIPE)\n",
        "  for line in p.stdout:\n",
        "    print(line.decode(), end='')\n",
        "\n",
        "\n",
        "threading.Thread(target=iframe_thread, daemon=True, args=(8188,)).start()\n",
        "\n",
        "!python main.py --dont-print-server"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "gggggggggg"
      },
      "source": [
        "### Run ComfyUI with colab iframe ***(use only in case the previous ways don't work)***\n",
        "\n",
        "You should see the ui appear in an iframe. If you get a 403 error, it's your firefox settings or an extension that's messing things up.\n",
        "\n",
        "If you want to open it in another window use the link.\n",
        "\n",
        "Note that some UI features like live image previews won't work because the colab iframe blocks websockets."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "hhhhhhhhhh",
        "cellView": "form"
      },
      "outputs": [],
      "source": [
        "#@title <font size=\"5\" color=\"orange\">ComfyUI iFrame</font>\n",
        "import threading\n",
        "import time\n",
        "import socket\n",
        "def iframe_thread(port):\n",
        "  while True:\n",
        "      time.sleep(0.5)\n",
        "      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
        "      result = sock.connect_ex(('127.0.0.1', port))\n",
        "      if result == 0:\n",
        "        break\n",
        "      sock.close()\n",
        "  from google.colab import output\n",
        "  output.serve_kernel_port_as_iframe(port, height=1024)\n",
        "  print(\"to open it in a window you can open this link here:\")\n",
        "  output.serve_kernel_port_as_window(port)\n",
        "\n",
        "threading.Thread(target=iframe_thread, daemon=True, args=(8188,)).start()\n",
        "\n",
        "!python main.py --dont-print-server"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "provenance": [],
      "private_outputs": true,
      "gpuType": "T4",
      "include_colab_link": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}